package cn.cttic.app.service.impl;

import cn.cttic.base.api.domain.BaseUser;
import cn.cttic.base.api.domain.bo.BaseUserBo;
import cn.cttic.base.api.domain.vo.BaseUserVo;
import cn.hutool.core.bean.BeanUtil;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.app.mapper.BaseUserMapper;
import cn.cttic.app.service.IBaseUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-06
 */
@RequiredArgsConstructor
@Service
public class BaseUserServiceImpl implements IBaseUserService {

    private final BaseUserMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public BaseUserVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<BaseUserVo> queryPageList(BaseUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseUser> lqw = buildQueryWrapper(bo);
        Page<BaseUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<BaseUserVo> queryList(BaseUserBo bo) {
        LambdaQueryWrapper<BaseUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseUser> buildQueryWrapper(BaseUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), BaseUser::getUserType, bo.getUserType());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), BaseUser::getUserName, bo.getUserName());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), BaseUser::getRealName, bo.getRealName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), BaseUser::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCardNo()), BaseUser::getIdCardNo, bo.getIdCardNo());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), BaseUser::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), BaseUser::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getSalt()), BaseUser::getSalt, bo.getSalt());
        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), BaseUser::getPassword, bo.getPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonId()), BaseUser::getPersonId, bo.getPersonId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(BaseUserBo bo) {
        BaseUser add = BeanUtil.toBean(bo, BaseUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(BaseUserBo bo) {
        BaseUser update = BeanUtil.toBean(bo, BaseUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
